#include "httpserver.h"

HttpServer* HttpServer::instance = nullptr;


HttpServer *HttpServer::getInstance()
{
    if(instance == nullptr){
        instance = new HttpServer();
    }

    return instance;
}

bool HttpServer::init()
{
    // 返回值表⽰绑定成功的端⼝号的值
    int ret = httpServer.listen(QHostAddress::Any, 8088);

    httpServer.route("/ping", [](const QHttpServerRequest& req){
        qDebug() << "httpServer 收到了ping请求";
        return "pong";
    });

    //获取个人信息
    httpServer.route("/service/user/get_user_info", [=](const QHttpServerRequest& req){
        return this->getUserInfo(req);
    });

    //获取好友列表
    httpServer.route("/service/friend/get_friend_list", [=](const QHttpServerRequest& req){
        return this->getFriendList(req);
    });

    //获取会话列表
    httpServer.route("/service/friend/get_chat_session_list", [=](const QHttpServerRequest& req){
        return getSessionList(req);
    });

    //获取好友申请列表
    httpServer.route("/service/friend/get_pending_friend_events", [=](const QHttpServerRequest& req){
        return getApplyList(req);
    });

    //获取最近消息
    httpServer.route("/service/message_storage/get_recent", [=](const QHttpServerRequest& req) {
        return this->getRecentMessages(req);
    });

    httpServer.route("/service/message_transmit/new_message", [=](const QHttpServerRequest& req) {
        return this->sendMessage(req);
    });

    httpServer.route("/service/user/set_nickname", [=](const QHttpServerRequest& req) {
        return this->setNickName(req);
    });

    httpServer.route("/service/user/set_description", [=](const QHttpServerRequest& req) {
        return this->setDescription(req);
    });

    httpServer.route("/service/user/get_phone_verify_code", [=](const QHttpServerRequest& req) {
        return this->getVerifyCode(req);
    });

    httpServer.route("/service/user/set_phone", [=](const QHttpServerRequest& req){
        return this->setPhone(req);
    });

    httpServer.route("/service/user/set_avatar", [=](const QHttpServerRequest& req) {
        return this->setAvatar(req);
    });

    httpServer.route("/service/friend/remove_friend", [=](const QHttpServerRequest& req) {
        return this->removeFriend(req);
    });

    httpServer.route("/service/friend/add_friend_apply", [=](const QHttpServerRequest& req) {
        return this->addFriendApply(req);
    });

    httpServer.route("/service/friend/add_friend_process", [=](const QHttpServerRequest& req) {
        return this->addAndRejectFriendProcess(req);
    });

    httpServer.route("/service/friend/create_chat_session", [=](const QHttpServerRequest& req) {
        return this->createChatSession(req);
    });

    httpServer.route("/service/friend/get_chat_session_member", [=](const QHttpServerRequest& req) {
        return this->getChatSessionMember(req);
    });

    httpServer.route("/service/friend/search_friend", [=](const QHttpServerRequest& req) {
        return this->searchFriend(req);
    });

    httpServer.route("/service/message_storage/search_history", [=](const QHttpServerRequest& req) {
        return this->searchHistory(req);
    });

    httpServer.route("/service/message_storage/get_history", [=](const QHttpServerRequest& req) {
        return this->getHistory(req);
    });

    httpServer.route("/service/user/username_login", [=](const QHttpServerRequest& req) {
        return this->userLogin(req);
    });

    httpServer.route("/service/user/username_register", [=](const QHttpServerRequest& req) {
        return this->userRegister(req);
    });

    httpServer.route("/service/user/phone_login", [=](const QHttpServerRequest& req) {
        return this->phoneLogin(req);
    });

    httpServer.route("/service/user/phone_register", [=](const QHttpServerRequest& req) {
        return this->phoneRegister(req);
    });

    return ret == 8088;
}

QHttpServerResponse HttpServer::getUserInfo(const QHttpServerRequest& req)
{
    lx::GetUserInfoReq Req;
    Req.deserialize(&serializer, req.body());
    LOG() << "[获取用户信息] requestId= " << Req.requestId() << ", loginSessionId= " << Req.sessionId();

    lx::GetUserInfoRsp Rsp;

    Rsp.setRequestId(Req.requestId());
    Rsp.setSuccess(true);
    Rsp.setErrmsg("");

    lx::UserInfo userInfo;
    userInfo.setUserId("1234");
    userInfo.setNickname("小明");
    userInfo.setDescription("生活如河，自己就是自己的船");
    userInfo.setPhone("12345678900");
    userInfo.setAvatar(loadImageToByteArray(":/images/default_avatar.jpg"));

    Rsp.setUserInfo(userInfo);

    //序列化
    QByteArray body = Rsp.serialize(&serializer);
    QHttpServerResponse httpRsp(body, QHttpServerResponse::StatusCode::Ok);
    httpRsp.addHeader("Content-Type", "application/x-protobuf");
    return httpRsp;

}

QHttpServerResponse HttpServer::getFriendList(const QHttpServerRequest &req)
{
    lx::GetFriendListReq Req;
    Req.deserialize(&serializer, req.body());
    LOG() << "[获取好友列表] reqeustId=" << Req.requestId() << ", loginSessionId=" << Req.sessionId();

    lx::GetFriendListRsp Rsp;
    Rsp.setRequestId(Req.requestId());
    Rsp.setSuccess(true);
    Rsp.setErrmsg("");

    for(int i = 0; i < 30; ++i){
        lx::UserInfo userInfo = makeUserInfo(i, loadImageToByteArray(":/images/default_avatar.jpg"));
        Rsp.friendList().push_back(userInfo);
    }

    QByteArray body = Rsp.serialize(&serializer);
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");

    return httpResp;
}

QHttpServerResponse HttpServer::getSessionList(const QHttpServerRequest &req)
{
    //解析请求
    lx::GetChatSessionListReq Req;
    Req.deserialize(&serializer, req.body());
    LOG() << "[获取会话列表] reqeustId=" << Req.requestId() << ", loginSessionId=" << Req.sessionId();

    //构造body
    lx::GetChatSessionListRsp Rsp;
    Rsp.setRequestId(Req.requestId());
    Rsp.setSuccess(true);
    Rsp.setErrmsg("");

    QByteArray avatar = loadImageToByteArray(":/images/default_avatar.jpg");

    for(int i = 0; i < 30; ++i){
        lx::ChatSessionInfo chatSessionInfo;
        chatSessionInfo.setChatSessionId(QString::number(i));
        chatSessionInfo.setChatSessionName("小明" + QString::number(i));

        if(i == 0){
            chatSessionInfo.setSingleChatFriendId("");
        }
        else{
            chatSessionInfo.setSingleChatFriendId(QString::number(i + 1000));
        }

        chatSessionInfo.setAvatar(avatar);

        lx::MessageInfo messageInfo = makeMessageInfo(i, chatSessionInfo.chatSessionId(), avatar);
        chatSessionInfo.setPrevMessage(messageInfo);

        Rsp.chatSessionInfoList().push_back(chatSessionInfo);
    }

    //序列化
    QByteArray body = Rsp.serialize(&serializer);

    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");

    return httpResp;
}

QHttpServerResponse HttpServer::getApplyList(const QHttpServerRequest &req)
{
    lx::GetPendingFriendEventListReq Req;
    Req.deserialize(&serializer, req.body());
    LOG() << "[获取好友申请列表] reqeustId=" << Req.requestId() << ", loginSessionId=" << Req.sessionId();

    lx::GetPendingFriendEventListRsp Rsp;
    Rsp.setRequestId(Req.requestId());
    Rsp.setSuccess(true);
    Rsp.setErrmsg("");

    QByteArray avatar = loadImageToByteArray(":/images/default_avatar.jpg");

    for(int i = 30; i < 60; ++i){
        lx::FriendEvent fe;
        fe.setEventId("event" + QString::number(i));
        lx::UserInfo userInfo = makeUserInfo(i, avatar);
        fe.setSender(userInfo);

        Rsp.event().push_back(fe);
    }

    //序列化
    QByteArray body = Rsp.serialize(&serializer);

    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");

    return httpResp;
}

QHttpServerResponse HttpServer::getRecentMessages(const QHttpServerRequest &req)
{
    int64_t beg = QDateTime::currentMSecsSinceEpoch();

    lx::GetRecentMsgReq Req;
    Req.deserialize(&serializer, req.body());
    LOG() << "[REQ 获取近期消息] request_id=" << Req.requestId() << ", sessionId=" << Req.sessionId();

    lx::GetRecentMsgRsp Rsp;
    Rsp.setRequestId(Req.requestId());
    Rsp.setSuccess(true);
    Rsp.setErrmsg("");

    QByteArray avatar = loadImageToByteArray(":/images/default_avatar.jpg");

    for (int i = 0; i < Req.msgCount(); i++) {
        lx::MessageInfo messageInfo = makeMessageInfo(i, Req.chatSessionId(), avatar);
        Rsp.msgList().push_back(messageInfo);
    }

    //序列化
    QByteArray body = Rsp.serialize(&serializer);

    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");

    int64_t end = QDateTime::currentMSecsSinceEpoch();
    LOG() << "[RESP] request_id=" << Req.requestId() << ", sessionId=" << Req.sessionId() << ", time=" << (end - beg);

    return httpResp;

}

QHttpServerResponse HttpServer::sendMessage(const QHttpServerRequest &req)
{
    // 解析请求
    lx::NewMessageReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 发送消息] request_id=" << pbReq.requestId() << ", sessionId=" << pbReq.sessionId();

    // 构造响应
    lx::NewMessageRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    QByteArray body = pbRsp.serialize(&serializer);

    // 发送响应给客⼾端
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::setNickName(const QHttpServerRequest &req)
{
    // 解析请求
    lx::SetUserNicknameReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 设置昵称] request_id=" << pbReq.requestId() << ", sessionId=" << pbReq.sessionId();

    // 构造响应
    lx::SetUserNicknameRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    QByteArray body = pbRsp.serialize(&serializer);

    // 发送响应给客⼾端
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::setDescription(const QHttpServerRequest &req)
{
    // 解析请求
    lx::SetUserDescriptionReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 设置昵称] request_id=" << pbReq.requestId() << ", sessionId=" << pbReq.sessionId();

    // 构造响应
    lx::SetUserDescriptionRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    QByteArray body = pbRsp.serialize(&serializer);

    // 发送响应给客⼾端
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::getVerifyCode(const QHttpServerRequest &req)
{
    // 解析请求
    lx::PhoneVerifyCodeReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 获取⼿机验证码] request_id=" << pbReq.requestId() << ", phone=" << pbReq.phoneNumber();

    // 构造响应
    lx::PhoneVerifyCodeRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    pbRsp.setVerifyCodeId("testVerifyCodeId");
    QByteArray body = pbRsp.serialize(&serializer);

    // 发送响应给客⼾端
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::setPhone(const QHttpServerRequest &req)
{
    // 解析请求
    lx::SetUserPhoneNumberReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 修改⼿机号码] request_id=" << pbReq.requestId() << ", phone="
          << pbReq.phoneNumber()
          << ", verifyCodeId=" << pbReq.phoneVerifyCodeId() << ", verifyCode="
          << pbReq.phoneVerifyCode();

    // 构造响应
    lx::SetUserPhoneNumberRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    QByteArray body = pbRsp.serialize(&serializer);

    // 发送响应给客⼾端
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::setAvatar(const QHttpServerRequest &req)
{
    // 解析请求
    lx::SetUserAvatarReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 修改头像] request_id=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId();

    // 构造响应
    lx::SetUserAvatarRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    QByteArray body = pbRsp.serialize(&serializer);

    // 发送响应给客⼾端
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::removeFriend(const QHttpServerRequest &req)
{
    // 解析请求
    lx::FriendRemoveReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 删除好友] request_id=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId()
        << ", peerId=" << pbReq.peerId();

    // 构造响应
    lx::FriendRemoveRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    QByteArray body = pbRsp.serialize(&serializer);

    // 发送响应给客⼾端
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::addFriendApply(const QHttpServerRequest &req)
{
    // 解析请求
    lx::FriendAddReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 发送好友申请] request_id=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId()
          << ", respondentId=" << pbReq.respondentId();

    // 构造响应
    lx::FriendAddRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    QByteArray body = pbRsp.serialize(&serializer);

    // 发送响应给客⼾端
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::addAndRejectFriendProcess(const QHttpServerRequest &req)
{
    // 解析请求
    lx::FriendAddProcessReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 处理好友申请] request_id=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId()
          << ", applyUserId=" << pbReq.applyUserId();

    // 构造响应
    lx::FriendAddProcessRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    QByteArray body = pbRsp.serialize(&serializer);

    // 发送响应给客⼾端
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::createChatSession(const QHttpServerRequest &req)
{
    // 解析请求
    lx::ChatSessionCreateReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 处理创建会话] request_id=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId();

    // 构造响应
    lx::ChatSessionCreateRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    lx::ChatSessionInfo chatSessionInfo;
    pbRsp.setChatSessionInfo(chatSessionInfo);
    QByteArray body = pbRsp.serialize(&serializer);

    // 发送响应给客⼾端
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::getChatSessionMember(const QHttpServerRequest &req)
{
    // 解析请求
    lx::GetChatSessionMemberReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 获取会话成员列表] request_id=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId() << ", chatSessionId=" << pbReq.chatSessionId();

    // 构造响应
    lx::GetChatSessionMemberRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    QByteArray avatar = loadImageToByteArray(":/images/default_avatar.jpg");

    auto& memberInfoList = pbRsp.memberInfoList();
    for (int i = 0; i < 10; ++i) {
        lx::UserInfo userInfo = makeUserInfo(1000 + i, avatar);
        memberInfoList.push_back(userInfo);
    }

    QByteArray body = pbRsp.serialize(&serializer);

    // 发送响应给客⼾端
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::searchFriend(const QHttpServerRequest &req)
{
    // 解析请求
    lx::FriendSearchReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 搜索⽤⼾] request_id=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId() << ", searchKey=" << pbReq.searchKey();

    // 构造响应
    lx::FriendSearchRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    QByteArray avatar = loadImageToByteArray(":/images/default_avatar.jpg");

    auto& userInfoList = pbRsp.userInfo();
    for (int i = 0; i < 10; ++i) {
        lx::UserInfo userInfo = makeUserInfo(1000 + i, avatar);
        userInfoList.push_back(userInfo);
    }

    QByteArray body = pbRsp.serialize(&serializer);

    // 发送响应给客⼾端
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::searchHistory(const QHttpServerRequest &req)
{
    // 解析请求
    lx::MsgSearchReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 搜索历史消息] request_id=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", chatSessionId=" << pbReq.chatSessionId() << ", searchKey=" << pbReq.searchKey();

    // 构造响应
    lx::MsgSearchRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    QByteArray avatar = loadImageToByteArray(":/images/default_avatar.jpg");

    auto& messageList = pbRsp.msgList();
    for (int i = 0; i < 10; ++i) {
        lx::MessageInfo messageInfo = makeMessageInfo(3000 + i, pbReq.chatSessionId(), avatar);
        messageList.push_back(messageInfo);
    }

    QByteArray body = pbRsp.serialize(&serializer);

    // 发送响应给客⼾端
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::getHistory(const QHttpServerRequest &req)
{
    // 解析请求
    lx::GetHistoryMsgReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 获取历史消息] request_id=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", chatSessionId=" << pbReq.chatSessionId();

    // 构造响应
    lx::GetHistoryMsgRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    QByteArray avatar = loadImageToByteArray(":/images/default_avatar.jpg");

    auto& messageList = pbRsp.msgList();
    for (int i = 0; i < 10; ++i) {
        lx::MessageInfo messageInfo = makeMessageInfo(3000 + i, pbReq.chatSessionId(), avatar);
        messageList.push_back(messageInfo);
    }

    QByteArray body = pbRsp.serialize(&serializer);
    // 发送响应给客⼾端
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::userLogin(const QHttpServerRequest &req)
{
    // 解析请求
    lx::UserLoginReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ ⽤⼾名登录] request_id=" << pbReq.requestId() << ", username=" << pbReq.nickname() << ", password=" << pbReq.password();

    // 构造响应
    lx::UserLoginRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    pbRsp.setLoginSessionId("testSessionId");

    QByteArray body = pbRsp.serialize(&serializer);

    // 发送响应给客⼾端
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::userRegister(const QHttpServerRequest &req)
{
    // 解析请求
    lx::UserRegisterReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ ⽤⼾名注册] request_id=" << pbReq.requestId() << ", username=" << pbReq.nickname() << ", password=" << pbReq.password();

    // 构造响应
    lx::UserRegisterRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    QByteArray body = pbRsp.serialize(&serializer);

    // 发送响应给客⼾端
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::phoneLogin(const QHttpServerRequest &req)
{
    // 解析请求
    lx::PhoneLoginReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ ⼿机号登录] request_id=" << pbReq.requestId() << ", phone=" << pbReq.phoneNumber()
          << ", verifyCodeId=" << pbReq.verifyCodeId() << ", verifyCode=" << pbReq.verifyCode();
    // 构造响应
    lx::PhoneLoginRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    pbRsp.setLoginSessionId("testSessionId");
    QByteArray body = pbRsp.serialize(&serializer);

    // 发送响应给客⼾端
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::phoneRegister(const QHttpServerRequest &req)
{
    // 解析请求
    lx::PhoneRegisterReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 电话注册] request_id=" << pbReq.requestId() << ", phone=" <<pbReq.phoneNumber()
          << ", verifyCodeId=" << pbReq.verifyCodeId() << ", verifyCode=" <<pbReq.verifyCode();
    // 构造响应
    lx::PhoneRegisterRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    QByteArray body = pbRsp.serialize(&serializer);
    // 发送响应给客⼾端
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

